package com.example.app06_component;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

/**
 * 测试DialogActivity
 *
 * @author lijiguo 
 */
public class DialogActivity extends AppCompatActivity {

    private static final String TAG = "DialogActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }

    /**
     * 显示一般Dialog
     * @param view 发生点击事件的视图对象
     */
    public void showAd(View view) {
        // new AlertDialog.Builder(this).create().show();
        new AlertDialog.Builder(this)
                .setTitle("删除数据") // 设置标题
                .setMessage("您确定要删除数据吗？") // 设置内容消息
                .setPositiveButton("确定", new DialogInterface.OnClickListener() { // 设置确定按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "AlertDialog.onClick: 确认按钮");
                        Toast.makeText(DialogActivity.this, "删除数据成功",
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() { // 设置取消按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "AlertDialog.onClick: 取消按钮");
                        Toast.makeText(DialogActivity.this, "取消删除",
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    /**
     * 显示Radio单选列表对话框
     * @param view 发生点击事件的视图对象
     */
    public void showRadioAd(View view) {
        final String[] colors = { "红", "绿", "蓝", "灰" };
        new AlertDialog.Builder(this)
                .setTitle("选择背景颜色")
                .setSingleChoiceItems(colors, 2, new DialogInterface.OnClickListener() {
                    /**
                     * 单选按钮点击事件
                     * @param dialog the dialog that received the click
                     * @param which the button that was clicked (ex.
                     *              {@link DialogInterface#BUTTON_POSITIVE}) or the position
                     *              of the item clicked
                     */
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "AlertDialog.onClick: " + colors[which]);
                        // which 选中的某一项的索引下标
                        Toast.makeText(DialogActivity.this, colors[which],
                                Toast.LENGTH_SHORT).show();
                        // 关闭Dialog
                        dialog.dismiss();
                    }
                })
                .show();
    }


    /**
     * 显示自定义对话框
     * @param view 发生点击事件的视图对象
     */
    public void showCustomAd(View view) {
        // 动态加载布局文件，得到对应的View对象
        // context 上下文对象
        // resource 布局文件
        // root ViewGroup 对象
        View dialogView = View.inflate(this, R.layout.dialog_view, null);
        new AlertDialog.Builder(this)
                .setView(dialogView) // 设置自定义布局
                .setNegativeButton("取消",  null)
                .setPositiveButton("登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取用户输入的用户名和密码并显示
                        EditText etUsername = dialogView.findViewById(R.id.et_dialog_view_username);
                        EditText etPassword = dialogView.findViewById(R.id.et_dialog_view_password);
                        Toast.makeText(DialogActivity.this, "用户名: " + etUsername.getText().toString()
                                + ", 密码: " + etPassword.getText().toString(), Toast.LENGTH_SHORT)
                                .show();
                    }
                })
                .show();
    }

    /**
     * 显示圆形进度Dialog
     * @param view 发生点击事件的视图对象
     */
    public void showCyclePd(View view) {
        ProgressDialog dialog = ProgressDialog.show(this, "数据加载", "数据加载中...");
        // 模拟数据加载，2秒钟后消失
        // 长时间不工作不能再主线程操作
        // 需要启动一个子线程
        // 否则会出现意想不到的结果
        // Thread.sleep(2000);
        new Thread(() -> {
            try {
                // 模拟一个异步请求任务
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // 关闭进度条
            // 此方法调用是在分线程，但是内部使用Handler实现主线程移除Dialog
            dialog.dismiss();
            // 不能在分线程中修改UI
            // 否则程序会崩溃
//            Toast.makeText(DialogActivity.this, "数据加载完成", Toast.LENGTH_SHORT)
//                    .show();
            // 在主线程执行
            runOnUiThread(() -> {
                // 更新UI
                Toast.makeText(DialogActivity.this, "数据加载完成", Toast.LENGTH_SHORT)
                    .show();
            });
        }).start();
    }

    /**
     * 显示水平进度Dialog
     * @param view 发生点击事件的视图对象
     */
    public void showHorPd(View view) {
        // 创建Dialog对象
        ProgressDialog dialog = new ProgressDialog(this);
        // 设置进度条的样式
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // 显示
        dialog.show();

        // 启动分线程，模拟数据加载
        new Thread(() -> {
            int count = 20;
            // 设置进度条的最大值
            dialog.setMax(count);
            for (int i = 0; i < count; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                dialog.setProgress(dialog.getProgress() + 1);
            }

            dialog.dismiss();

            // 在主线程执行
            runOnUiThread(() -> {
                // 更新UI
                Toast.makeText(DialogActivity.this, "数据加载完成", Toast.LENGTH_SHORT)
                        .show();
            });
        }).start();
    }

    /**
     * 显示日期选择对话框
     * @param view 发生点击事件的视图对象
     */
    public void showDateDialog(View view) {
        // 创建日历对象
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        Log.i(TAG, "showDateDialog: " + year + "-" + month + "-" + dayOfMonth);

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Log.i(TAG, "onDateSet: " + year + "-" + month + "-" + dayOfMonth);
            }
        }, year, month, dayOfMonth).show();
    }

    /**
     * 显示事件选择对话框
     * @param view 发生点击事件的视图对象
     */
    public void showTimeDialog(View view) {
        // 创建日历对象
        Calendar calendar = Calendar.getInstance();
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        Log.i(TAG, "showTimeDialog: " + hourOfDay + ":" + minute);

        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Log.i(TAG, "onTimeSet: " + hourOfDay + ":" + minute);
            }
        }, hourOfDay, minute, true).show();
    }
}
